﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlatformerCS
{
    /// <summary>
    /// Basic camera that follows the player.
    /// Allow simple operations such as zoom and dezoom.
    /// </summary>
    class Camera2D
    {
        private Matrix transform;
        int mouseWheel, prevMouseWheel;
        float scale;

        KeyboardState Kstate, prevKstate;

        public Matrix Transform
        {
            get { return transform; }
        }

        public Camera2D()
        {
            mouseWheel = Mouse.GetState().ScrollWheelValue;
            scale = 1f;
        }

        public void Update(Vector2 position, Point size)
        {
            prevKstate = Kstate;
            prevMouseWheel = mouseWheel;
            Kstate = Keyboard.GetState();
            mouseWheel = Mouse.GetState().ScrollWheelValue;

            // Zooming & dezooming
            if (mouseWheel > prevMouseWheel)
                scale += 0.05f;
            else if (mouseWheel < prevMouseWheel)
                scale -= 0.05f;

            UpdateTransform(position, size);
        }

        private void UpdateTransform(Vector2 position, Point size)
        {
            position -= new Vector2(GameBoard.WIDTH / 2 - size.X / 2, GameBoard.HEIGHT / 2 - size.Y - 2);

            transform = Matrix.Identity *
                        Matrix.CreateTranslation(-position.X, -position.Y, 0) *
                        Matrix.CreateScale(new Vector3(scale, scale, scale));
        }

        public void Draw(SpriteBatch spriteBatch)
        {

        }
    }
}
